//
//  SNHBaseCenterActionSheet.m
//  SNHActionSheetKit
//
//  Created by 陈建蕾 on 2018/6/1.
//

#import "SNHBaseCenterActionSheet.h"
#import <SNHActionSheetKit/SNHFeedbackGenerator.h>

static NSString *KSNHAnimationKeyPath = @"KSNHAnimationKeyPath";

@interface SNHBaseCenterActionSheet ()
@property (nonatomic, assign) CGSize containSize;
@end

@implementation SNHBaseCenterActionSheet

+ (instancetype)actionSheetWithSize:(CGSize)size {
    return [[self alloc] initWithSize:size];
}

- (instancetype)initWithSize:(CGSize)size {
    if (self = [super initWithFrame:CGRectZero]) {
        _containSize = size;
        [self _config];
        [self viewDidLoad];
    }
    return self;
}

- (void)_config {
    _isTapMiss = YES;
    _duration = 0.4f;
    _keyPath = @"transform";
    _values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    _keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    _timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
}

- (void)viewDidLoad {
    self.actionSheetView = [[UIView alloc]initWithFrame:CGRectZero];
    self.actionSheetView.opaque = YES;
    self.actionSheetView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.actionSheetView];
}

- (void)showInWindow {
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [SNHFeedbackGenerator light];
}

- (void)showInSuperView:(UIView *)superView {
    if (superView) {
        [superView addSubview:self];
        [SNHFeedbackGenerator light];
    }
}
- (void)miss {
    [self.actionSheetView.layer removeAnimationForKey:KSNHAnimationKeyPath];
    [self removeFromSuperview];
}

#pragma mark - Private
- (void)didMoveToSuperview {
    if (self.superview) {
        self.frame = self.superview.bounds;
        self.actionSheetView.frame = CGRectMake((CGRectGetWidth(self.bounds) - self.containSize.width) * 0.5, (CGRectGetHeight(self.bounds) - self.containSize.height) * 0.5, self.containSize.width, self.containSize.height);
        
        CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:self.keyPath];
        popAnimation.duration = self.duration;
        popAnimation.values = self.values;
        popAnimation.keyTimes = self.keyTimes;
        popAnimation.timingFunctions = self.timingFunctions;
        [self.actionSheetView.layer addAnimation:popAnimation forKey:KSNHAnimationKeyPath];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    if (!CGRectContainsPoint(self.actionSheetView.frame, touchPoint)) {
        if (self.isTapMiss) {
            [self miss];
        }
    }
}

@end
